package com.example.smssender;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.google.android.material.textfield.TextInputEditText;
import java.util.List;

/**
 * 短信内容生成器Activity
 */
public class SmsContentGeneratorActivity extends AppCompatActivity {
    
    public static final String RESULT_GENERATED_CONTENT = "generated_content";
    
    private RadioGroup rgSceneType;
    private RecyclerView rvScenes;
    private TextInputEditText etGeneratedContent;
    private Button btnRegenerate;
    private Button btnUseContent;
    
    private SceneAdapter sceneAdapter;
    private List<SmsContentGenerator.Scene> currentScenes;
    private SmsContentGenerator.Scene selectedScene;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sms_content_generator);
        
        initViews();
        setupToolbar();
        setupRecyclerView();
        setupRadioGroup();
        setupButtons();
        
        // 默认加载节日祝福场景
        loadScenesByType(SmsContentGenerator.SceneType.FESTIVAL_GREETING);
    }
    
    private void initViews() {
        rgSceneType = findViewById(R.id.rgSceneType);
        rvScenes = findViewById(R.id.rvScenes);
        etGeneratedContent = findViewById(R.id.etGeneratedContent);
        btnRegenerate = findViewById(R.id.btnRegenerate);
        btnUseContent = findViewById(R.id.btnUseContent);
    }
    
    private void setupToolbar() {
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        }
        toolbar.setNavigationOnClickListener(v -> finish());
    }
    
    private void setupRecyclerView() {
        rvScenes.setLayoutManager(new LinearLayoutManager(this));
        sceneAdapter = new SceneAdapter(null, scene -> {
            selectedScene = scene;
            generateContent();
        });
        rvScenes.setAdapter(sceneAdapter);
    }
    
    private void setupRadioGroup() {
        rgSceneType.setOnCheckedChangeListener((group, checkedId) -> {
            // 避免初始化时触发
            if (checkedId == -1) {
                return;
            }
            SmsContentGenerator.SceneType type = getSceneTypeFromRadioButton(checkedId);
            if (type != null) {
                loadScenesByType(type);
            }
        });
    }
    
    private void setupButtons() {
        btnRegenerate.setOnClickListener(v -> {
            if (selectedScene != null) {
                generateContent();
            } else {
                Toast.makeText(this, "请先选择一个场景", Toast.LENGTH_SHORT).show();
            }
        });
        
        btnUseContent.setOnClickListener(v -> {
            String content = etGeneratedContent.getText().toString().trim();
            if (content.isEmpty()) {
                Toast.makeText(this, "请先生成内容", Toast.LENGTH_SHORT).show();
                return;
            }
            
            // 返回生成的内容
            Intent resultIntent = new Intent();
            resultIntent.putExtra(RESULT_GENERATED_CONTENT, content);
            setResult(RESULT_OK, resultIntent);
            finish();
        });
    }
    
    private SmsContentGenerator.SceneType getSceneTypeFromRadioButton(int checkedId) {
        if (checkedId == R.id.rbFestivalGreeting) {
            return SmsContentGenerator.SceneType.FESTIVAL_GREETING;
        } else if (checkedId == R.id.rbNotification) {
            return SmsContentGenerator.SceneType.NOTIFICATION;
        } else if (checkedId == R.id.rbMarketing) {
            return SmsContentGenerator.SceneType.MARKETING;
        } else if (checkedId == R.id.rbReminder) {
            return SmsContentGenerator.SceneType.REMINDER;
        } else if (checkedId == R.id.rbApology) {
            return SmsContentGenerator.SceneType.APOLOGY;
        } else if (checkedId == R.id.rbThanks) {
            return SmsContentGenerator.SceneType.THANKS;
        } else if (checkedId == R.id.rbInvitation) {
            return SmsContentGenerator.SceneType.INVITATION;
        } else if (checkedId == R.id.rbCongratulation) {
            return SmsContentGenerator.SceneType.CONGRATULATION;
        }
        return null;
    }
    
    private void loadScenesByType(SmsContentGenerator.SceneType type) {
        currentScenes = SmsContentGenerator.getScenesByType(type);
        sceneAdapter.updateScenes(currentScenes);
        
        // 清空之前的选择和内容
        selectedScene = null;
        etGeneratedContent.setText("");
        
        // 如果场景列表不为空，自动选择第一个并生成内容
        if (currentScenes != null && !currentScenes.isEmpty()) {
            selectedScene = currentScenes.get(0);
            generateContent();
        }
    }
    
    private void generateContent() {
        if (selectedScene != null) {
            String content = SmsContentGenerator.generateContent(selectedScene);
            if (content != null && etGeneratedContent != null) {
                etGeneratedContent.setText(content);
            }
        }
    }
}

